/*
 * @Descripttion: 
 * @version: 
 * @Author: wangzhilei
 * @Date: 2021-09-21 00:56:06
 * @LastEditors: wangzhilei
 * @LastEditTime: 2021-09-21 02:02:16
 */

#include "../config.h"

int main(int argc,char **argv)
{
    struct sockaddr_in s_addr;
    struct sockaddr_in c_addr;
    int sock;
    socklen_t addr_len;
    int len;
    char buff[128];

    if ((sock=socket(AF_INET,SOCK_DGRAM,0))==-1)
    {
        perror("socket");
        exit(EXIT_FAILURE);
    }else
    {
        printf("create socket.\n\r");
    }
    
    bzero(&s_addr,sizeof(s_addr));
    s_addr.sin_addr.s_addr=INADDR_ANY;
    s_addr.sin_family=AF_INET;
    s_addr.sin_port=htons(PORT);

    if (bind(sock,(struct sockaddr *)&s_addr,sizeof(s_addr)))
    {
        perror("bind err");
        exit(EXIT_FAILURE);
    }else
        printf("bind address to socket.\n\t");

    addr_len=sizeof(c_addr);
    while (1)
    {
        len=recvfrom(sock,buff,sizeof(buff)-1,0,(struct sockaddr *) &c_addr,&addr_len);
        if (len<0)
        {
            perror("recvfrom");
            exit(EXIT_FAILURE);
        }
        buff[len]='\0';
        printf("recive come from %s:%d message:%s\n\r",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port),buff);
        return 0;
    }
    
    
    
}